package com.yicong.ants.ui.video.adapter;

import static com.yicong.ants.utils.UIUtils.showToast;

import com.cchao.simplelib.core.RxHelper;
import com.cchao.simplelib.ui.adapter.StatefulBindQuickAdapter;
import com.cchao.simplelib.ui.interfaces.BaseView;
import com.kennyc.view.MultiStateView;
import com.yicong.ants.BR;
import com.yicong.ants.Constants;
import com.yicong.ants.R;
import com.yicong.ants.bean.video.VideoListBean;
import com.yicong.ants.manager.http.RetrofitHelper;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.disposables.CompositeDisposable;

/**
 * @author LinDingQiang
 * @description
 * @time 4/21/21 2:08 PM
 * @email dingqiang.l
 */
public class VideoHomeAdapter extends StatefulBindQuickAdapter<VideoListBean> {
    CompositeDisposable mDisposable;
    // 0 喜欢点赞视频 1 转发视频 2 自己的作品
    String mType = "";
    String mUserId = "";
    BaseView mBaseView;

    public VideoHomeAdapter(BaseView baseView, CompositeDisposable disposable) {
        super(R.layout.video_home_item);
        mDisposable = disposable;
        mBaseView = baseView;
    }

    public VideoHomeAdapter setType(String type) {
        mType = type;
        return this;
    }

    public VideoHomeAdapter setUserId(String userId) {
        mUserId = userId;
        return this;
    }

    @Override
    protected void convert(DataBindViewHolder helper, VideoListBean item) {
        helper.getBinding().setVariable(BR.bean, item);
    }

    @Override
    public void loadPageData(int page) {
        final Map<String, String> params = new HashMap<>();
        params.put("page", String.valueOf(page));
        params.put("page_size", String.valueOf(Constants.PAGE_SIZE));
        params.put("type", mType);
        params.put("user_id", mUserId);

        if (page == 1) {
            setViewState(MultiStateView.VIEW_STATE_LOADING);
        }
        mDisposable.add(RetrofitHelper.getApis().getVideoHomeList(params)
            .compose(RxHelper.toMain())
            .subscribe(respBean -> {
                if (respBean.isCodeFail()) {
                    showToast(respBean.getMsg());
                    if (page == 1) {
                        setViewState(MultiStateView.VIEW_STATE_ERROR);
                    }
                    return;
                }
                solveData(respBean.getData(), page, Constants.PAGE_SIZE);
            }, RxHelper.getSwitchableErrorConsumer(this)));
    }
}
